package com.edu.udistrital.indexing.analyzer;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.Set;

import org.apache.lucene.analysis.CharArraySet;
import org.apache.lucene.analysis.ReusableAnalyzerBase;
import org.apache.lucene.analysis.WordlistLoader;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.Version;

public abstract class Analyzer extends ReusableAnalyzerBase {
	
	protected final Version MATCH_VERSION;
	protected final CharArraySet STOPWORDS;
	
	public Analyzer(Version version, String stopWordFile) {
		this.MATCH_VERSION = version;
		
		File fileStopWords = new File(stopWordFile);
		
		if(!fileStopWords.exists()) {
			this.STOPWORDS = CharArraySet.EMPTY_SET;
		} else {
			this.STOPWORDS = CharArraySet
							.unmodifiableSet(CharArraySet.copy(MATCH_VERSION, cargarStopWordSet(fileStopWords, MATCH_VERSION)));
		}	
	}

	protected CharArraySet cargarStopWordSet(File stopWordsFile, Version version) {
		Reader reader = null;
		try {
			Charset charset = Charset.forName("ISO-8859-1");
			reader = IOUtils.getDecodingReader(stopWordsFile, charset);
			 return WordlistLoader.getWordSet(reader, MATCH_VERSION);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		    try {
				IOUtils.close(reader);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;	
	}
	
	protected Set<?> getStopWordSet() {
		return STOPWORDS;
	}
}
